package a51qindian.com.comprehensive.base;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.li.base.BaseActivity;
import com.li.interfaces.OnTopNavigationBarListener;
import com.li.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import a51qindian.com.comprehensive.App;
import a51qindian.com.comprehensive.util.SpUtil;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/9/12 0012
 * Description:
 */

public abstract class QDBaseActivity extends BaseActivity implements OnTopNavigationBarListener,OnRefreshLoadmoreListener {


    protected String mUid;
    protected Context mContext;
    protected Activity mActivity;
    protected int mPage=0;
    protected boolean mIsRefresh=true;
    private SmartRefreshLayout mSmartRefreshLayout;
    private List<Disposable> mDisposableList;

    @Override
    protected void initSth() {
        ButterKnife.bind(this);
        App.addActivity(this);
        mContext=this;
        mActivity=this;
        mUid= SpUtil.getUserInfo(this,SpUtil.USER_ID);
    }

    @Override
    public void onBackClickListener(View view) {
        finish();
    }

    @Override
    public void onFunctionImgListener(View view) {

    }

    @Override
    public void onFunctionTxtListener(View view) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancelAllDisposable();
        App.removeActivity(this);
    }


    protected void addDisposable(Disposable disposable) {
        if (mDisposableList == null) {
            mDisposableList = new ArrayList<>();
        }
        mDisposableList.add(disposable);
    }

    protected void cancelAllDisposable() {
        if (mDisposableList == null) {
            return;
        }
        for (Disposable disposable : mDisposableList) {
            if (disposable.isDisposed()) {
                continue;
            }
            disposable.dispose();
        }
    }

    protected void setSmartRefreshLayout(SmartRefreshLayout smartRefreshLayout){
        mSmartRefreshLayout=smartRefreshLayout;
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
    }
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mIsRefresh=false;
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mIsRefresh=true;
    }
    protected void stopRefreshLoad(){
        mSmartRefreshLayout.finishRefresh();
        mSmartRefreshLayout.finishLoadmore();
        mSmartRefreshLayout.setLoadmoreFinished(false);
    }
    protected void showToast(String content){
        if (TextUtils.isEmpty(content)) {
            return;
        }
        ToastUtil.showToast(mContext,content);
    }
    protected boolean checkIsEmpty(EditText editText){
        return TextUtils.isEmpty(editText.getText().toString());
    }
}
